﻿using System.Net.Http.Headers;

namespace BlzOidc.Data
{
    public class WeatherForecastApiClient
    {
        private readonly HttpClient _httpClient;
        private readonly TokenProvider _tokenProvider;
        private readonly TokenManager _tokenManager;

        public WeatherForecastApiClient(HttpClient httpClient, TokenProvider tokenProvider, TokenManager tokenManager)
        {
            _httpClient = httpClient;
            _tokenProvider = tokenProvider;
            _tokenManager = tokenManager;
        }

        public async Task<WeatherForecast[]?> GetForecastAsync()
        {
            //var token = _tokenProvider.AccessToken;
            //获取有效的Access Token
            string token = await _tokenManager.GetValidAccessTokenAsync();
            _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

            var result = await _httpClient.GetFromJsonAsync<WeatherForecast[]>("/WeatherForecast");

            return result;
        }
    }
}
